package com.ruoyi.web.controller.client;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.client.QualityDataDetails;
import com.ruoyi.system.service.IQualityDataDetailsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 质量数据采集明细Controller
 *
 * @author ruoyi
 * @date 2023-02-20
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@Api(tags = "质量数据采集明细api")
@RequestMapping("/qualityData/details" )
public class QualityDataDetailsController extends BaseController {

    private final IQualityDataDetailsService qualityDataDetailsService;

    /**
     * 查询质量数据采集明细列表
     */
    @ApiOperation(value = "查询质量数据采集明细列表")
    @GetMapping("/list")
    public TableDataInfo list(QualityDataDetails qualityDataDetails)
    {
        startPage();
        LambdaQueryWrapper<QualityDataDetails> lqw = new LambdaQueryWrapper<QualityDataDetails>();
        if (StringUtils.isNotBlank(qualityDataDetails.getAssemblyCode())){
            lqw.like(QualityDataDetails::getAssemblyCode ,qualityDataDetails.getAssemblyCode());
        }
        if (StringUtils.isNotBlank(qualityDataDetails.getOrderCode())){
            lqw.like(QualityDataDetails::getOrderCode ,qualityDataDetails.getOrderCode());
        }
        if (StringUtils.isNotBlank(qualityDataDetails.getSerialNumber())){
            lqw.eq(QualityDataDetails::getSerialNumber ,qualityDataDetails.getSerialNumber());
        }

        List<QualityDataDetails> list = qualityDataDetailsService.list(lqw);
        return getDataTable(list);
    }

    /**
     * 导出质量数据采集明细列表
     */
    @Log(title = "质量数据采集明细" , businessType = BusinessType.EXPORT)
    @ApiOperation(value = "导出质量数据采集明细列表")
    @GetMapping("/export" )
    public AjaxResult export(QualityDataDetails qualityDataDetails) {
        LambdaQueryWrapper<QualityDataDetails> lqw = new LambdaQueryWrapper<QualityDataDetails>(qualityDataDetails);
        List<QualityDataDetails> list = qualityDataDetailsService.list(lqw);
        ExcelUtil<QualityDataDetails> util = new ExcelUtil<QualityDataDetails>(QualityDataDetails. class);
        return util.exportExcel(list, "质量数据采集明细" );
    }

    /**
     * 获取质量数据采集明细详细信息
     */
    @ApiOperation(value = "获取质量数据采集明细详细信息")
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(qualityDataDetailsService.getById(id));
    }

    /**
     * 新增质量数据采集明细
     */
    @Log(title = "质量数据采集明细" , businessType = BusinessType.INSERT)
    @ApiOperation(value = "新增质量数据采集明细")
    @PostMapping
    public AjaxResult add(@RequestBody QualityDataDetails qualityDataDetails) {
        return toAjax(qualityDataDetailsService.save(qualityDataDetails) ? 1 : 0);
    }

    /**
     * 修改质量数据采集明细
     */
    @Log(title = "质量数据采集明细" , businessType = BusinessType.UPDATE)
    @ApiOperation(value = "修改质量数据采集明细")
    @PutMapping
    public AjaxResult edit(@RequestBody QualityDataDetails qualityDataDetails) {
        return toAjax(qualityDataDetailsService.updateById(qualityDataDetails) ? 1 : 0);
    }

    /**
     * 删除质量数据采集明细
     */
    @Log(title = "质量数据采集明细" , businessType = BusinessType.DELETE)
    @ApiOperation(value = "删除质量数据采集明细")
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(qualityDataDetailsService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
